package com.e3.basdoc.corp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.corp.dao.CorpDAO;
import com.e3.basdoc.corp.entity.CorpEntity;
import com.e3.basdoc.corp.model.CorpModel;
import com.e3.basdoc.corp.model.CorpQC;
import com.e3.basdoc.corp.service.CorpQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class CorpQueryServiceImpl extends SuperQueryService implements CorpQueryService {

	private CorpDAO corpDAO;

	private CorpDAO getCorpDAO() {
		if (corpDAO == null) {
			corpDAO = DAOHelper.createDAOInstance(CorpDAO.class);
		}
		return corpDAO;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpQueryService#getCorpModelById(long)
	 */
	public CorpModel getCorpModelById(long id) throws ServiceException {
		CorpEntity corp = getCorpDAO().getById(CorpEntity.class, id);
		return corpEntityToModel(corp);
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpQueryService#getCorpModelByIds(java.lang.Long)
	 */
	public List<CorpModel> getCorpModelByIds(Long... ids) throws ServiceException {
		List<CorpEntity> corpEntities = getCorpDAO().findBySingleField(CorpEntity.class, "id", ids);

		if (corpEntities != null && corpEntities.size() > 0) {
			List<CorpModel> corpModels = new ArrayList<CorpModel>(corpEntities.size());
			for (CorpEntity corpEntity : corpEntities) {
				corpModels.add(corpEntityToModel(corpEntity));
			}
			return corpModels;
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpQueryService#getCorpByCondition(com.e3.basdoc.corp.model.CorpQC)
	 */
	public List<CorpModel> getCorpByCondition(CorpQC corpQC) throws ServiceException {
		List<CorpModel> corpModels = new ArrayList<CorpModel>();
		List<CorpEntity> corps = getCorpDAO().getCorpsByCondition(corpQC);
		if (corps != null) {
			for (CorpEntity corp : corps) {
				corpModels.add(corpEntityToModel(corp));
			}
		}
		return corpModels;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpQueryService#getCorpChildren(com.e3.basdoc.corp.model.CorpModel)
	 */
	public List<CorpModel> getCorpChildren(CorpModel corpModel) throws ServiceException {
		
		Long id = corpModel==null?0L:corpModel.getId();
		List<CorpEntity> entities = getCorpDAO().findBySingleField(CorpEntity.class, "parentId", new Long[] { id });

		if (entities != null) {
			List<CorpModel> corpModels = new ArrayList<CorpModel>(entities.size());
			for (CorpEntity entity : entities) {
				CorpModel corpMd = corpEntityToModel(entity);
				corpModels.add(corpMd);
			}
			return corpModels;
		}

		return new ArrayList<CorpModel>(0);
	}
	

	public CorpModel getSimpleCorpByCode(String code) throws ServiceException {
		CorpEntity entity = getCorpDAO().getSimpleCorpByCode(code);
		if(entity!=null){
			CorpModel model = new CorpModel();
			model.setId(entity.getId());
			model.setCode(entity.getCode());
			model.setName(entity.getName());
			model.setShortName(entity.getShortName());
			return model;
		}
		return null;
	}

	private CorpModel corpEntityToModel(CorpEntity corp) {
		CorpModel corpModel = new CorpModel();
		corpModel = super.entityToModel(corpModel, corp);
		corpModel.setId(corp.getId());

		// 基本信息
		corpModel.setCode(corp.getCode());
		corpModel.setName(corp.getName());
		corpModel.setShortName(corp.getShortName());
		corpModel.setType(corp.getType() + "");
		corpModel.setParentId(corp.getParentId());
		corpModel.setRegionId(corp.getRegionId());
		corpModel.setBusinessAddress(corp.getBusinessAddress());
		corpModel.setContactAddress(corp.getContactAddress());
		corpModel.setFax(corp.getFax());
		corpModel.setZipcode(corp.getZipcode());
		corpModel.setTelphone(corp.getTelphone());
		corpModel.setEmail(corp.getEmail());
		corpModel.setRemark(corp.getRemark());

		// 注册信息
		corpModel.setRegisterAddress(corp.getRegisterAddress());
		corpModel.setLegalPerson(corp.getLegalPerson());
		corpModel.setLegalPersonIdcard(corp.getLegalPersonIdcard());
		corpModel.setRegisterNo(corp.getRegisterNo());
		corpModel.setTaxNo(corp.getTaxNo());
		corpModel.setContactPerson(corp.getContactPerson());
		corpModel.setContactWay(corp.getContactWay());
		corpModel.setFoundTime(new Date(corp.getFoundTime()));
		corpModel.setRegisterStartTime(new Date(corp.getRegisterStartTime()));
		corpModel.setRegisterEndTime(new Date(corp.getRegisterEndTime()));
		corpModel.setCurrencyId(corp.getCurrencyId());
		corpModel.setTaxpayerType(corp.getTaxpayerType() + "");

		// 其他信息
		corpModel.setEstablishAccount(corp.getEstablishAccount());
		corpModel.setMaxUsers(corp.getMaxUsers() + "");
		corpModel.setMaxBranchs(corp.getMaxBranchs() + "");
		corpModel.setStartTime(new Date(corp.getStartTime()));
		corpModel.setEndTime(new Date(corp.getEndTime()));

		long count = getCorpDAO().childrenCount(corp.getId());
		corpModel.setHasChildren(count > 0);

		return corpModel;
	}


	
}
